<?php
/**
 * XIE--Scorpion
 */

namespace App\Http\Controllers\admin;

use App\Models\Admin\User;
use App\Models\admin\Postoffice;
use DB,Session,Url,Input,Request,Redirect;
use App\Http\Controllers\Controller;
/**
* 后台主页
*/
class PostofficeController extends Controller
{
	/*
	 *后台发布职位列表
	 */
	 public function postofficeList()
	{
		//调用Mode层
        $arr=Postoffice::postofficelists();  
		//print_r($arr);die;
		//传值，遍历显示
		return view('admin/postofficeList',compact('arr'));
	}
	/*
	 *后台发布职位
	 */
	 public function postofficeAdd()
	{

		if($_GET){

		   $arr=$_GET;

		   if(Postoffice::postofficeadds($arr)){

               return redirect('/admin/postofficeList');

			}else{
                echo "<script>alert('发布失败');<script>";

			    return view('admin/postofficeadd');

			}
		}else{

           $data=DB::table('position')->get();
		   $arr['position']=$this->digui($data,$position_parent=0,$level=0);
		   $arr['enterprise']=Session::get('eid');
		   $arr['city']=DB::table('city')->get();
		   $arr['pay']=DB::table('pay')->get();
		   $arr['background']=DB::table('background')->get();
		   $arr['suffer']=DB::table('suffer')->get();

		   return view('admin/postofficeadd',compact('arr'));

		}				
	}
    /*
     *无限级分类
     */
    public function digui($data,$position_parent=0)
	{
        $child=array();
        foreach($data as $key=>$val)
		{
            if($val->position_parent == $position_parent)
			{
                $child[]=$val;
            }
        }
        foreach($child as $key1=>$v)
		{
            $current_child=$this->digui($data,$v->position_id);
            $child[$key1]->child=$current_child;
        }
        return $child;
    }
	/*
	 *后台发布职位列表删除
	 */
	 public function postofficeDelete()
	{
		//接收id
		$message_id=$_GET['message_id'];
		//print_r($message_id);die;
		//调用Mode层
		if(Postoffice::postofficedeletes($message_id)){

           return redirect('/admin/postofficeList');

		}else{
		   echo "<script>alert('删除失败');<script>";

		   return view('admin/postofficeadd');

		}
	}
	/*
	 *后台发布职位列表修改查数据
	 */
	 public function postofficeUpdate()
	{
		if($_POST)
		{
            //接收id
			$message_id=isset($_POST['message_id'])?$_POST['message_id']:'';
			$arr['position_id']=isset($_POST['position_id'])?$_POST['position_id']:'';
			$arr['position_names']=isset($_POST['position_names'])?$_POST['position_names']:'';
			$arr['pay_id']=isset($_POST['pay_id'])?$_POST['pay_id']:'';
			$arr['enterprise_id']=isset($_POST['enterprise_id'])?$_POST['enterprise_id']:'';
			$arr['city_id']=isset($_POST['city_id'])?$_POST['city_id']:'';
			$arr['suffer_id']=isset($_POST['suffer_id'])?$_POST['suffer_id']:'';
			$arr['background_id']=isset($_POST['background_id'])?$_POST['background_id']:'';
			$arr['position_lure']=isset($_POST['position_lure'])?$_POST['position_lure']:'';
			$arr['position_duty']=isset($_POST['position_duty'])?$_POST['position_duty']:'';
			$arr['position_require']=isset($_POST['position_require'])?$_POST['position_require']:'';
			//print_r($message_id);die;
			//调用Mode层
			
			if(Postoffice::postofficesaves($message_id,$arr)){

                return redirect('/admin/postofficeList');

		    }else{
		       echo "<script>alert('修改失败');<script>";

		       return view('admin/postofficeList');

		    }
		}else{
            //接收id
			$message_id=$_GET['message_id'];
			//print_r($message_id);die;
			//调用Mode层
			$arr['post_message']=Postoffice::postofficeupdates($message_id);
			$data=DB::table('position')->get();
			$arr['position']=$this->digui($data,$position_parent=0,$level=0);
			$arr['enterprise']=DB::table('enterprise')->get();
			$arr['city']=DB::table('city')->get();
			$arr['pay']=DB::table('pay')->get();
			$arr['background']=DB::table('background')->get();
			$arr['suffer']=DB::table('suffer')->get();
			
			return view('admin/postofficeupdate',compact('arr'));
		}
		
	}
}